 @include "gleelib"

 ##### input variables:
 #      K = column number of class. if negative,
 #          counts from the right-hand-side.
 #          Defaults to "-1" (right-hand-column)
 # 

 ##### generates
 # Has[cxt][row]             : "row" is a row in context "cxt"
 # Klass[cxt][k]             : Context "cxt" has a klas "k"
 # Name[col]                 : text name of column "col"
 # Data[row][col]            : some value, stored in "row", "col"

 #--- generates, for symbols
 # Sym[col]                  : column "col" contains symbols
 # F[cxt,col,klass][val]     : frequency of "val" in some context's symbolic "col", for "klass" 
 # Fall[cxt,col,klass]       : total of all entries in some context's symbolic "col", for "klass" 

 #-- generates, for numerics
 # Num[col]                  : column "col" contains numbers
 # N[cxt,col,klass]          : number of "col" entries in "klass" of "cxt"
 # Sum[cxt,col,klass]        : sum of "col" entries in "klass" of "cxt"
 # SumSq[cxt,col,klass]      : sum of squared "col" entries in "klass" of "cxt"
 # Value[cxt,col,klass][row] : all values
 # Max[cxt,col,klass]        : max of these item
 # Min[cxt,col,klass]        : min of these items

 BEGIN         { IGNORECASE = 1  }
 /@relation/   { Relation   = $2 }
 /@attribute/  { A++; Name[A]=$2; 
                 $3 ~ /(numeric|integer|real|float)/ ? Num[A] : Sym[A]
               }
 /@data/       { In=1; FS=","; C=0; 
                 if (K) {
                    K = K>0 ? K : K += A + 1
                 } else { K = A }
                 next }
 /@/           { next }
               { gsub(/%.*/,""); gsub(/[ \t'\"]*/,"");  if (/^$/) next }
 In && NF==A   { cells(C,initRow()) }
 
 BEGIN {_R=0}  
 function initRow(    col) {
     _R++
     for(col=1;col<=A;col++) Data[_R, col] = $col
     return _R
 } # end _R
 function cells(c,row,     klass,col,val,key1,key2) {
     klass = Data[row,K]
     Has[c][row] 
     Klass[c][klass]
     for(col in Name) {
         key1 = c _ col _ klass # cache keys for faster load times
         key2 = c _ col _ "_"   
         if ((val = Data[row,col]) != Missing)   
	     if (col in Sym) {
		 symCell(key1,val)
		 symCell(key2,val)
	     } else {
		 numCell(key1,val,row)
		 numCell(key2,val,row)
 }}}
 function symCell(key,val) {
     F[key][val]++
     Fall[key]++
 }
 function numCell(key,val,row) {
     N[key]++
     Sum[key]   += val
     SumSq[key] += val * val
     Value[key][row]=val
     if (key in Max) { if (val > Max[key]) Max[key] = val } else Max[key] = val
     if (key in Min) { if (val < Min[key]) Min[key] = val } else Min[key] = val
 }
 function readArffDump(    i,j,k) {
     print "K=" K
     for(i in Klass) for(j in Klass[i]) print "cluster " i " has klass " j
     o(Name,"name")
     o(Max, "max")
     o(Min, "min")
     o(N,"N")
     o(Sum,"Sum") ; o(SumSq,"SumSq"); 
     for(i in F) 
	 for (j in F[i]) {
	     k = i; gsub(_,";",k)
	     print "F[ " k " ][ " j " ] = " F[i][j] | "sort -k 4"
	 }
     o(Fall,"Fall")
 }